import { IApi } from '@umijs/types';
import axios from 'axios';
import {
  DefaultConfigModal,
  requestModal,
  ResultPromiseFnType,
  AuthResultModal,
} from '../modal';
export const request = (
  api: IApi,
  auth: AuthResultModal,
): ResultPromiseFnType | null => {
  const config = api.config.hwAuth as DefaultConfigModal;
  const configReq = config.request;
  if (!configReq) {
    return null;
  }
  if (typeof configReq === 'function') {
    return configReq(auth);
  }
  const { url, method = 'POST', params = {} } = <requestModal>configReq;
  // @ts-ignore
  const authStr = JSON.stringify(auth, '', '\t');
  return () => {
    return axios({
      url,
      method,
      params: { ...params, data: authStr },
    });
  };
};
